package com.study.mybatis.service;

import com.generate.beans.User;
import com.generate.mappers.UserMapper;
import com.study.mybatis.main.DBTools;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class ServericeGenerate {
    public static void main(String[] args) {
//        insert();
//        deleteUser();
//        forInsert();
        bathInsert();
    }

    private static void bathInsert(){
        ArrayList<User> list = new ArrayList<User>();
        for (int i = 0; i < 10000; i++) {
            list.add(new User(i, "admin_" + i, "123", 5f));
        }
        long start = System.currentTimeMillis();
        SqlSession session = DBTools.getSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.batchInsert(list);

        System.out.println("bathInsert takes " + (System.currentTimeMillis() - start) + "  ms");
    }

    private static void forInsert(){
        ArrayList<User> list = new ArrayList<User>();
        for (int i = 0; i < 10000; i++) {
            list.add(new User(i, "admin_" + i, "123", 5f));
        }
        long start = System.currentTimeMillis();
        SqlSession session = DBTools.getSession(false);
        UserMapper mapper = session.getMapper(UserMapper.class);
        list.forEach(mapper::insert);
        session.commit();
        System.out.println("forInsert takes " + (System.currentTimeMillis() - start) + "  ms");
    }

    private static void insert() {
        SqlSession session = DBTools.getSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.insert(new User(1, "test", "admin", 7.5f));
        session.close();
    }

    private static void deleteUser() {
        SqlSession session = DBTools.getSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.deleteUser(1);
        session.close();
    }
}
